package de.karlNet.sambamanager.view.beans.forms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Controller;

import de.karlNet.login.Useraccountcontrol;
import de.karlNet.sambamanager.ldapController.commands.macros.CreateUserMacro;
import de.karlNet.sambamanager.ldapModel.LdapUser;
import de.karlNet.sambamanager.ldapModel.dto.LdapUserCreationDTO;
import de.karlNet.sambamanager.view.beans.ManualLeafHandlingTreeNode;
import de.karlNet.sambamanager.view.beans.trees.SelectedNodeContainer;

@Controller
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class CreateUserBean extends ViewBean {
	@Autowired
	private SelectableGroupTree groupTree;

	protected LdapUserCreationDTO ldapUserCreationDTO = new LdapUserCreationDTO();

	@Autowired
	private CreateUserMacro createUserMacro;
	@Autowired
	protected SelectedNodeContainer nodeContainer;

	public void setAccountDisabled(boolean accountDisabled) {
		if (accountDisabled) {
			this.ldapUserCreationDTO.getLdapUser().getUseraccountcontrols()
					.add(Useraccountcontrol.ACCOUNT_DISABLED);
		} else {
			this.ldapUserCreationDTO.getLdapUser().getUseraccountcontrols()
					.remove(Useraccountcontrol.ACCOUNT_DISABLED);
		}
	}

	public boolean getAccountDisabled() {
		return this.ldapUserCreationDTO.getLdapUser().getUseraccountcontrols()
				.contains(Useraccountcontrol.ACCOUNT_DISABLED);
	}

	public void setPasswordDoNotExpire(boolean passwordDoNotExpire) {
		if (passwordDoNotExpire) {
			this.ldapUserCreationDTO.getLdapUser().getUseraccountcontrols()
					.add(Useraccountcontrol.DONT_EXPIRE_PASSWORD);
		} else {
			this.ldapUserCreationDTO.getLdapUser().getUseraccountcontrols()
					.remove(Useraccountcontrol.DONT_EXPIRE_PASSWORD);
		}
	}

	public boolean getPasswordDoNotExpire() {
		return this.ldapUserCreationDTO.getLdapUser().getUseraccountcontrols()
				.contains(Useraccountcontrol.DONT_EXPIRE_PASSWORD);
	}

	public void setCannotChangePassword(boolean cannotChangePassword) {
		if (cannotChangePassword) {
			this.ldapUserCreationDTO.getLdapUser().getUseraccountcontrols()
					.add(Useraccountcontrol.PASSWORD_CANT_CHANGE);
		} else {
			this.ldapUserCreationDTO.getLdapUser().getUseraccountcontrols()
					.remove(Useraccountcontrol.PASSWORD_CANT_CHANGE);
		}
	}

	public boolean getCannotChangePassword() {
		return this.ldapUserCreationDTO.getLdapUser().getUseraccountcontrols()
				.contains(Useraccountcontrol.PASSWORD_CANT_CHANGE);
	}

	public String create() {
		try {
			this.ldapUserCreationDTO.getLdapUser().setCn(
					this.ldapUserCreationDTO.getLdapUser().getFirstname()
							+ " "
							+ this.ldapUserCreationDTO.getLdapUser()
									.getLastname());
			this.ldapUserCreationDTO.getLdapUser().setDn(
					"cn="
							+ this.ldapUserCreationDTO.getLdapUser().getCn()
							+ ","
							+ this.nodeContainer.selectedNode.getLdapObject()
									.getDn());
			this.ldapUserCreationDTO.getMemberShips().addAll(
					this.groupTree.getSelectedGroups());
			this.createUserMacro.createUser(ldapUserCreationDTO);
			if (this.nodeContainer.selectedNode.isExpanded()) {
				new ManualLeafHandlingTreeNode("user",
						this.ldapUserCreationDTO.getLdapUser(),
						this.nodeContainer.selectedNode);
			}
			this.ldapUserCreationDTO = new LdapUserCreationDTO();
			this.groupTree.reset();
			this.setShow(false);

			return "index.xhtml";
		} catch (Exception e) {
			this.showErrorMessage(e.getMessage());
			return "";
		}
	}

	@Override
	public void setShow(boolean show) {
		this.groupTree.reset();

		super.setShow(show);
	}

	public LdapUser getLdapUser() {
		return ldapUserCreationDTO.getLdapUser();
	}

	public LdapUserCreationDTO getLdapUserCreationDTO() {
		return ldapUserCreationDTO;
	}

	@Override
	public String getPageName() {
		return "createUserView";
	}
}
